package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import util.Game;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户输入数据模块：
 * 解析request数据
 * 检查数据的有效性
 * 对game对象list数据进行更新操作，
 * 同时更新视图
 */
@Controller
@RequestMapping("")
public class InputDataController {
    private Game game = Game.getInstance();

    @RequestMapping("input")

    public ModelAndView show() {
        ModelAndView mav = new ModelAndView();
        //解析request数据 存入Game实例 更新view
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Integer a = 1, b = 2, c = 3, d = 4;
        try {
            a = Integer.valueOf(request.getParameter("a"));
            b = Integer.valueOf(request.getParameter("b"));
            c = Integer.valueOf(request.getParameter("c"));
            d = Integer.valueOf(request.getParameter("d"));

        } catch (Exception e) {

        }
        game.setDigitAt(0, a);
        game.setDigitAt(1, b);
        game.setDigitAt(2, c);
        game.setDigitAt(3, d);
        mav.addObject("a", game.getDigitAt(0));
        mav.addObject("b", game.getDigitAt(1));
        mav.addObject("c", game.getDigitAt(2));
        mav.addObject("d", game.getDigitAt(3));


        mav.addObject("status", "输入数据为" + game.getDigits().subList(0, 4));
        mav.setViewName("index");
        return mav;

    }
}
